<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/8
 * Time: 10:44
 */

namespace app\admin\controller;

use app\common\controller\Backend;

class Website extends Backend
{
    public function configure(){
       $companyInfo = model('register') -> where(['uid' => session('userinfo:id')]) -> find();
       $userInfo = model('userinfo') -> where(['uid' => session('userinfo:id')]) -> find();
       if(!empty($companyInfo['fengcai_img1'])){
           $companyInfo['fengcai_img1'] = explode('$', $companyInfo['fengcai_img1']);
       }
       if(!empty($companyInfo['fengcai_img2'])){
           $companyInfo['fengcai_img2'] = explode('$', $companyInfo['fengcai_img2']);
       }
       if(!empty($companyInfo['fengcai_img3'])){
           $companyInfo['fengcai_img3'] = explode('$', $companyInfo['fengcai_img3']);
       }
       if(!empty($companyInfo['fengcai_img4'])){
           $companyInfo['fengcai_img4'] = explode('$', $companyInfo['fengcai_img4']);
       }
       if(!empty($companyInfo['fengcai_img5'])){
           $companyInfo['fengcai_img5'] = explode('$', $companyInfo['fengcai_img5']);
       }
       if(!empty($companyInfo['fengcai_img6'])){
           $companyInfo['fengcai_img6'] = explode('$', $companyInfo['fengcai_img6']);
       }

       if(!empty($companyInfo['anli_img1'])){
           $companyInfo['anli_img1'] = explode('$', $companyInfo['anli_img1']);
       }
       if(!empty($companyInfo['anli_img2'])){
           $companyInfo['anli_img2'] = explode('$', $companyInfo['anli_img2']);
       }
       if(!empty($companyInfo['anli_img3'])){
           $companyInfo['anli_img3'] = explode('$', $companyInfo['anli_img3']);
       }
       if(!empty($companyInfo['anli_img4'])){
           $companyInfo['anli_img4'] = explode('$', $companyInfo['anli_img4']);
       }
       if(!empty($companyInfo['anli_img5'])){
           $companyInfo['anli_img5'] = explode('$', $companyInfo['anli_img5']);
       }
       if(!empty($companyInfo['anli_img6'])){
           $companyInfo['anli_img6'] = explode('$', $companyInfo['anli_img6']);
       }

       $this -> assign('userinfo', $userInfo);
       $this -> assign('companyinfo', $companyInfo);
       return view();
    }
    //文化
    public function editCulture(){
        $construction = input('post.');
        // 过滤javascript与CSS
        $construction['culture'] = preg_replace('/<script[^>]*?>.*?<\/script>/si','',$construction['culture']);
        $construction['culture'] = htmlspecialchars_decode($construction['culture']);
        $construction['culture'] = preg_replace("/<a[^>]*>(.*?)<.*?\/a.*?>/is", "$1", $construction['culture']);

        $result = model('userinfo') -> where(['uid' => session('userinfo:id')]) -> update($construction);
        if($result){
            return json(['status' => 1, 'info' => '更新成功！']);
        }
        return json(['status' => 0, 'info' => '更新失败！']);
    }
    //七牛上传文件
    public function qiniu() {
        $file = $this -> request -> file('myfile');
        $filePath = $file -> getInfo('tmp_name');

        $ext = pathinfo($file -> getInfo('name'), PATHINFO_EXTENSION);
        $key = 'upload/'.date('Ymdhis').substr(md5($file -> getRealPath()), 0, 5). rand(0, 999).'.'.$ext;

        vendor('qiniu.autoload');
        $accessKey = config('ACCESSKEY');
        $secretKey = config('SECRETKEY');
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        $bucket = config('BUCKET');
        $domain = config('domain');
        // 生成上传Token
        $token = $auth->uploadToken($bucket);
        $uploaderMgr = new \Qiniu\Storage\UploadManager();
        //调用putFile方法上传文件
        list($ret, $err) = $uploaderMgr -> putFile($token, $key, $filePath);
        if($err !== null) {
            return json(['status' => 0, 'info' => $err]);
        }else {
            return json(['status' => 1, 'info' => '上传成功！', 'data' => '//'.$domain.'/'.$ret['key']]);
        }
    }
    //更改简介
    public function editJianjie(){
        $construction = input('post.');
        // 过滤javascript与CSS
        $construction['construction'] = preg_replace('/<script[^>]*?>.*?<\/script>/si','',$construction['construction']);
        $construction['construction'] = htmlspecialchars_decode($construction['construction']);
        $construction['construction'] = preg_replace("/<a[^>]*>(.*?)<.*?\/a.*?>/is", "$1", $construction['construction']);

        $result = model('userinfo') -> where(['uid' => session('userinfo:id')]) -> update($construction);
        if($result){
            return json(['status' => 1, 'info' => '更新成功！']);
        }
        return json(['status' => 0, 'info' => '更新失败！']);
    }
    //更改风采
    public function editFengcai(){
        $fengcai = input('post.');
        $result = model('register') -> where(['uid' => session('userinfo:id')]) -> update($fengcai);
        if($result){
            return json(['status' => 1, 'info' => '更新成功！']);
        }
        return json(['status' => 0, 'info' => '更新失败！']);
    }
    //更改资质
    public function editZizhi(){
        $zhizi = input('post.');
        $result = model('register') -> where(['uid' => session('userinfo:id')]) -> update($zhizi);
        if($result){
            return json(['status' => 1, 'info' => '更新成功！']);
        }
        return json(['status' => 0, 'info' => '更新失败！']);
    }
    //更改案例
    public function editAnli(){
        $anli = input('post.');
        $result = model('register') -> where(['uid' => session('userinfo:id')]) -> update($anli);
        if($result){
            return json(['status' => 1, 'info' => '更新成功！']);
        }
        return json(['status' => 0, 'info' => '更新失败！']);
    }
    //联系我们更改
    public function editLianxi(){
        $lianxi = input('post.');
        $result = model('userinfo') -> where(['uid' => session('userinfo:id')]) -> update($lianxi);
        if($result){
            return json(['status' => 1, 'info' => '更新成功！']);
        }
        return json(['status' => 0, 'info' => '更新失败！']);
    }
}